package com.jae.controller;

import com.jae.model.ResultMsg;
import com.jae.model.SecurityUser;
import com.jae.model.ServiceRuntimeException;
import com.jae.model.user.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class BaseController {
    public ResultMsg fail(ServiceRuntimeException e) {
        return new ResultMsg(false, e.getMessage());
    }


    public ResultMsg error(String interfaceName, Exception e, HttpServletRequest request){
        e.printStackTrace();
        ResultMsg resultMsg = new ResultMsg(false,"请求异常");
        //可以加个线程收集异常到数据库，方便监控
        return resultMsg;
    }

    protected User getUser() {
        try {
            SecurityUser userDetails = (SecurityUser) SecurityContextHolder.getContext().getAuthentication()
                    .getPrincipal();

            User user = userDetails.getUser();
            log.debug("【用户:】:" + user);

            return user;
        } catch (Exception e) {
        }
        return null;
    }
}
